//
//  YbrJsonUtil.m
//  wifibox
//
//  Created by os X 10.9 on 15-1-11.
//  Copyright (c) 2015年 yinbaner. All rights reserved.
//

#import "YbrJsonUtil.h"
#import "PrintObject.h"

@implementation YbrJsonUtil

+(NSString *) getJson:(id) object {
    NSDictionary *dict = [PrintObject getObjectData:object];
    
    NSString *jsonString = [[NSString alloc] initWithData:[YbrJsonUtil toJSONData:dict] encoding:NSUTF8StringEncoding];
    return jsonString;
}

+ (NSData *)toJSONData:(id)theData{
    NSError *error = nil;

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData options:NSJSONWritingPrettyPrinted error:&error];
    if ([jsonData length] > 0 && error == nil){
        return jsonData;
    }else{
        return nil;
    }
}

// 下面都是通过系统的JSON类直接转换的
+(NSString *) ToSystemJson:(id) object {
    NSData *jsonData    =   [NSJSONSerialization dataWithJSONObject:object options:nil error:nil];
    NSString *json      =   [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return json;
}

+(id) ToObjBySystem:(NSString *) str {
    id jsonObject = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
    return jsonObject;
}


@end
